from rest_framework import serializers
from .models import CourseCategory, Course, Teacher, CourseLesson, CourseChapter


class CourseCategoryModelSerializer(serializers.ModelSerializer):
    class Meta:
         model = CourseCategory
         fields = ['id', 'name']

class TeacherModelSerializer(serializers.ModelSerializer):
    """课程所属老师的序列化器"""
    class Meta:
        model = Teacher
        fields = ["id", "name", "title", "signture", "brief", "image"]

class CourseModelSerializer(serializers.ModelSerializer):
    """课程信息的序列化器"""
    teacher = TeacherModelSerializer()
    class Meta:
        model = Course
        fields = ["id", "name", "students", "lessons", "pub_lessons", "price", "course_img", "teacher",
                  "lessons_list", "discount_name","real_price"]


class CourseRetrieveModelSerializer(serializers.ModelSerializer):
    """详情页课程信息序列化器"""
    teacher = TeacherModelSerializer()  # 外连表获取的是表的id,要想获取表信息,要序列化器嵌套
    class Meta:
        model = Course
        fields = ["id", "name", "students", "lessons", "pub_lessons", "price", "course_img"
                  , "teacher", "level_name", "brief_html", "course_video", "discount_name", "real_price", "activity_time"]


class CourseLessonModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseLesson
        fields = ["id","lesson","name","free_trail"]

class CourseChapterModelSerializer(serializers.ModelSerializer):
    """详情页课程章节列表"""
    coursesections = CourseLessonModelSerializer(many=True)
    class Meta:
        model = CourseChapter
        fields = ["id", "chapter", "name", "coursesections"]

